<?php
include('../data/comm.inc.php');
include('../data/myadminvar.php');
include('../func/func.php');
include("../func/csfunc.php");
include('../func/adminfunc.php');
include('../include.php');
include('./checklogin.php');
switch ($_REQUEST['xtype']) {
    case "show":
        if ($_REQUEST['gid'] != '')
            $gid = $_REQUEST['gid'];
        $msql->query("select * from `$tb_game` where gid='$gid' ");
        $msql->next_record();
        $game                   = array();
        $game[0]['gid']         = $msql->f('gid');
        $game[0]['gname']       = $msql->f('gname');
        $game[0]['fast']        = $msql->f('fast');
        $game[0]['baostatus']   = $msql->f('baostatus');
        $game[0]['autokj']      = $msql->f('autokj');
        $game[0]['autoopenpan'] = $msql->f('autoopenpan');
        $game[0]['panstatus']   = $msql->f('panstatus');
        $game[0]['otherstatus'] = $msql->f('otherstatus');
        $game[0]['thisqishu']   = $msql->f('thisqishu');
        
        $game[0]['thisbml'] = $msql->f('thisbml');
        $msql->query("select * from `$tb_game` where gid!='$gid' and ifopen=1 order by xsort");
        $i = 1;
        while ($msql->next_record()) {
            $game[$i]['gid']   = $msql->f('gid');
            $game[$i]['gname'] = $msql->f('gname');
            $game[$i]['fast']  = $msql->f('fast');
            $i++;
        }
        $sdate = week();
        $tpl->assign("sdate", $sdate);
        $tpl->assign('game', $game);
        $tpl->display("kj.html");
        break;
    case "padd":
        $gid   = $_POST['gid'];
        $pdate = $_POST['pdate'];
        if (!preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $pdate))
            exit;
        paddqishu($gid, $pdate);
        echo 1;
        break;
    case "add":
        $gid = $_POST['gid'];
        if ($gid == 111 | $gid == 115 | $gid == 133) {
            echo 1;
            exit;
        }
        $opentime  = $_POST['opentime'];
        $closetime = $_POST['closetime'];
        $kjtime    = $_POST['kjtime'];
        $qishu     = $_POST['qishu'];
        $msql->query("select thisbml from `$tb_game` where gid='$gid'");
        $msql->next_record();
        $bml       = $msql->f('thisbml');
        $editstart = str_replace(':', '', $config['editstart']);
        if (str_replace(':', '', substr($kjtime, -8)) < $editstart) {
            $dates = sqldate(strtotime($kjtime));
        } else {
            $dates = substr($kjtime, 0, 10);
        }
        $sql = "insert into `$tb_kj` set kjtime='$kjtime',opentime='$opentime',closetime='$closetime',qishu='$qishu',dates='$dates',bml='$bml',gid='$gid',baostatus=1";
        $msql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
        $msql->next_record();
        if ($msql->f(0) != 1) {
            $msql->query($sql);
        }
        echo 1;
        break;
    case "getkj":
        $gid      = $_POST['gid'];
        $jsstatus = $_POST['jsstatus'];
        $psize    = $_POST['psize'];
        $page     = $_POST['page'];
        $start    = $_POST['start'];
        $end      = $_POST['end'];
        $ze       = $_POST['ze'];
        //$start    = strtotime($start . ' ' . $config['editend']);
        //$end      = strtotime($end . ' ' . $config['editstart']) + 86400;
        
        //$start    = sqltime($start);
        //$end      = sqltime($end);
        $fast = transgame($gid, 'fast');
        $time = time();
        if ($fast == 1) {
            if (!is_numeric($page))
                $page = 1;
            if ($jsstatus == 0) {
                $whi     = " and js=0 and dates>='$start' and dates<='$end'";
                $orderby = " order by dates,kjtime ";
            } else if ($jsstatus == 1) {
                $whi     = "  and dates>='$start' and dates<='$end' and UNIX_TIMESTAMP(kjtime)-600<$time ";
                $orderby = " order by dates desc,kjtime desc ";
            } else {
                $whi     = " and dates>='$start' and dates<='$end'  ";
                $orderby = " order by dates,kjtime ";
            }
        } else {
            $orderby = " order by kjtime  desc ";
        }
        //echo "select count(id) from `$tb_kj` where gid='$gid' $whi ";
        $msql->query("select count(id) from `$tb_kj` where gid='$gid' $whi ");
        $msql->next_record();
        $rcount = $msql->f(0);
        $msql->query("select * from `$tb_kj` where gid='$gid' $whi $orderby limit " . (($page - 1) * $psize) . ",$psize");
        $i              = 0;
        $kj             = array();
        $otherclosetime = $config['otherclosetime'];
        $tmp            = array();
        while ($msql->next_record()) {
            if ($tmp['g' . $msql->f('gid')] == '') {
                $tmp['g' . $msql->f('gid')] = transgame($msql->f('gid'), 'sgname');
            }
            if ($ze == 1) {
                $fsql->query("select 1 from `$tb_lib` where gid='" . $msql->f('gid') . "' and qishu='" . $msql->f('qishu') . "'  limit 1");
                $fsql->next_record();
                if ($fsql->f(0) != 1) {
                    continue;
                }
            }
            $kj[$i]['gname']          = $tmp['g' . $msql->f('gid')];
            $kj[$i]['gid']            = $msql->f('gid');
            $kj[$i]['bml']            = $msql->f('bml');
            $kj[$i]['oy']             = substr($msql->f('opentime'), 0, 4);
            $kj[$i]['cy']             = substr($msql->f('closetime'), 0, 4);
            $kj[$i]['ky']             = substr($msql->f('kjtime'), 0, 4);
            $kj[$i]['opentime']       = date("m-d H:i:s", strtotime($msql->f('opentime')));
            $kj[$i]['closetime']      = date("m-d H:i:s", strtotime($msql->f('closetime')));
            $kj[$i]['kjtime']         = date("m-d H:i:s", strtotime($msql->f('kjtime')));
            $kj[$i]['otherclosetime'] = date("m-d H:i:s", strtotime($msql->f('closetime')) - $otherclosetime);
            $kj[$i]['baostatus']      = $msql->f('baostatus');
            $kj[$i]['js']             = $msql->f('js');
            $kj[$i]['qishu']          = $msql->f('qishu');
            $kj[$i]['lib']            = getlibje($msql->f('gid'), $msql->f('qishu'));
            for ($j = 1; $j <= $config['mnum']; $j++) {
                $kj[$i]['m' . $j] = $msql->f('m' . $j);
            }
            $i++;
        }
        $pcount = $rcount % $psize == 0 ? $rcount / $psize : (1 + ($rcount - $rcount % $psize) / $psize);
        echo json_encode(array(
            "kj" => $kj,
            'pcount' => $pcount,
            'rcount' => $rcount,
            'mnum' => $config['mnum']
        ));
        break;
    case "upkj":
        
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        $qishu = explode('|', $qishu);
        $cq    = count($qishu);
        include_once("../func/csfunc.php");
        $mnum = transgame($gid, 'mnum');
        $cs   = json_decode(transgame($gid, 'cs'), true);
        for ($i = 0; $i < $cq; $i++) {
            if ($qishu[$i] != '') {
                if ($cs['randbm'] == 1) {
                    $ma = kjmyself($qishu[$i], $mnum);
                    $ma = json_decode($ma, true);
                    if ($ma[0]['m'][0] == '')
                        continue;
                    //print_r($ma);
                    $sql = "update `$tb_kj` set ";
                    for ($j = 1; $j <= $mnum; $j++) {
                        if ($j > 1)
                            $sql .= ",";
                        $sql .= "m" . $j . "='" . $ma[0]["m"][$j - 1] . "'";
                    }
                    $sql .= " where qishu='" . $qishu[$i] . "' and gid='$gid'";
                    //echo $sql;
                    $msql->query($sql);
                    continue;
                }
                $qs = formatqs($gid, $qishu[$i]);
                if ($gid == 163) {
                    $ma = file_get_contents("http://" . $config['kjip'] . "&gid=161&qishu=" . $qs);
                    
                    $ma = json_decode($ma, true);
                    if (!is_array($ma[0]['m'])) {
                        $ma[0]['m'] = explode(',', $ma[0]['m']);
                    }
                    if ($ma[0]['m'][0] == '')
                        continue;
                    $m   = xy28kj($ma[0]['m']);
                    $sql = "update `$tb_kj` set ";
                    for ($j = 1; $j <= 3; $j++) {
                        $sql .= "m" . $j . "='" . $m[$j - 1] . "',";
                    }
                    $sql .= "kjtime='" . $ma[0]['kjtime'] . "' where qishu='" . $qishu[$i] . "' and gid='$gid'";
                } else {
                    if ($gid == 152) {
                        $ma = file_get_contents("http://" . $config['kjip'] . "&gid=155&qishu=" . $qs);
                    } else {
                        $ma = file_get_contents("http://" . $config['kjip'] . "&gid=$gid&qishu=" . $qs);
                    }
                    
                    $ma = json_decode($ma, true);
                    if (!is_array($ma[0]['m'])) {
                        $ma[0]['m'] = explode(',', $ma[0]['m']);
                    }
                    if ($ma[0]['m'][0] == '')
                        continue;
                    $sql = "update `$tb_kj` set ";
                    for ($j = 1; $j <= $mnum; $j++) {
                        $sql .= "m" . $j . "='" . $ma[0]["m"][$j - 1] . "',";
                    }
                    $sql .= "kjtime='" . $ma[0]['kjtime'] . "' where qishu='" . $qishu[$i] . "' and gid='$gid'";
                }
                $msql->query($sql);
            }
        }
        echo 1;
        break;
    case "setthisqishu":
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        $msql->query("update `$tb_game` set thisqishu='$qishu' where gid='$gid'");
        echo 1;
        break;
    case "delall":
        if ($_SESSION['admin'] != 1)
            exit;
        if ($_POST['pass'] != $config['supass']) {
            echo 2;
            exit;
        }
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        include('../data/cuncu.php');
        $kksql->query($deletestr);
        $msql->query("delete from `$tb_kj` where instr('$qishu',qishu) and gid='$gid'");
        $msql->query("delete from `$tb_lib` where instr('$qishu',qishu) and gid='$gid'");
        //$msql->query("delete from `$tb_z` where instr('$qishu',qishu) and gid='$gid'");
        $kksql->query($deletecc);
        echo 1;
        break;
    case "delbao":
        if ($_SESSION['admin'] != 1)
            exit;
        if ($_POST['pass'] != $config['supass']) {
            echo 2;
            exit;
        }
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        include('../data/cuncu.php');
        $kksql->query($deletestr);
        $msql->query("delete from `$tb_lib` where instr('$qishu',qishu) and gid='$gid'");
        //$msql->query("delete from `$tb_z` where instr('$qishu',qishu) and gid='$gid'");
        $kksql->query($deletecc);
        echo 1;
        break;
    case "deldate":
        if ($_SESSION['admin'] != 1)
            exit;
        if ($_POST['pass'] != $config['supass']) {
            echo 2;
            exit;
        }
        
        $gid     = $_POST['gid'];
        $start   = $_POST['start'];
        $end     = $_POST['end'];
        $t       = $_POST['t'];
        $allgame = $_POST['allgame'];
        //$start   = strtotime($start . ' ' . $config['editend']);
        //$end     = strtotime($end . ' ' . $config['editstart']) + 86400;
        //$start   = sqltime($start);
        //$end     = sqltime($end);
        if ($allgame == 0) {
            $gamestr           = "and  `$tb_kj`.gid='$gid' ";
            $gamearr           = array();
            $gamearr[0]        = array();
            $gamearr[0]['gid'] = $gid;
        } else {
            $gamearr = getgame();
        }
        $cg = count($gamearr);
        include('../data/cuncu.php');
        $kksql->query($deletestr);
        for ($i = 0; $i < $cg; $i++) {
            if ($gamearr[$i]['gid'] == 100 & $cg > 1)
                continue;
            $gamestr = " gid='" . $gamearr[$i]['gid'] . "' ";
            if ($t == 1) {
                $whi = " $gamestr and dates>='$start' and dates<='$end' ";
            } else if ($t == 2) {
                $whi = " $gamestr and dates<='$end' ";
            }
            if ($t == 1 | $t == 2) {
                $msql->query("delete from `$tb_lib` where $whi");
                $msql->query("delete from `$tb_kj`  where $whi ");
            }
        }
        $kksql->query($deletecc);
        echo 1;
        break;
    
    case "changebaos":
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        $msql->query("update `$tb_kj` set baostatus=if(baostatus=1,0,1) where gid='$gid' and qishu='$qishu'");
        $msql->query("select baostatus from `$tb_kj` where  gid='$gid' and qishu='$qishu'");
        $msql->next_record();
        $bs = $msql->f('baostatus');
        $msql->query("update `$tb_lib` set bs='$bs' where gid='$gid' and qishu='$qishu'");
        echo $bs;
        break;
    case "changebaostatus":
        $gid       = $_POST['gid'];
        $start     = $_POST['start'];
        $end       = $_POST['end'];
        $baostatus = $_POST['baostatus'];
        $start     = strtotime($start . ' ' . $config['editend']);
        $end       = strtotime($end . ' ' . $config['editstart']) + 86400;
        $msql->query("update `$tb_kj` set baostatus='$baostatus' where gid='$gid' and kjtime>=$start and kjtime<=$end");
        $msql->query("update `$tb_lib` set bs='$baostatus' where gid='$gid' and time>=$start and time<=$end");
        echo 1;
        break;
    case "changejs":
        $gid   = $_POST['gid'];
        $qishu = $_POST['qishu'];
        $msql->query("select js from `$tb_kj` where gid='$gid' and qishu='$qishu'");
        $msql->next_record();
        $js = $msql->f('js');
        if ($js == 0) {
            echo $js;
            exit;
        }
        include('../data/cuncu.php');
        $kksql->query($updatestr);
        $msql->query("update `$tb_lib` set z=9 where gid='$gid' and qishu='$qishu'");
        $kksql->query($updatecc);
        
        $msql->query("update `$tb_kj` set js=0 where gid='$gid' and qishu='$qishu'");
        $msql->query("delete from `$tb_z` where gid='$gid' and qishu='$qishu'");
        jiaozhengedu();
        echo 0;
        break;
    case "updatestatus":
        $type = $_POST['type'];
        $gid  = $_POST['gid'];
        $msql->query("update `$tb_game` set $type=if($type=1,0,1) where gid='$gid'");
        echo 1;
        break;
    
    case "editkj":
        $gid       = $_POST['gid'];
        $qishu     = $_POST['qishu'];
        $kjtime    = $_POST['kjtime'];
        $closetime = $_POST['closetime'];
        $opentime  = $_POST['opentime'];
        $m         = str_replace('\\', '', $_POST['em']);
        $m         = json_decode($m, true);
        $mstr      = '';
        for ($i = 0; $i < $config['mnum']; $i++) {
            if ($m[$i] == '') {
                if ($config['mnum'] == 3) {
                    $arr   = array(
                        0,
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                        8,
                        9
                    );
                    $m[$i] = $arr[rand(0, 9)];
                } else if ($config['mnum'] == 5) {
                    $arr   = array(
                        0,
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                        8,
                        9
                    );
                    $m[$i] = $arr[rand(0, 9)];
                } else if ($config['mnum'] == 8) {
                    $arr   = array(
                        "01",
                        "02",
                        "03",
                        "04",
                        "05",
                        "06",
                        "07",
                        "08",
                        "09",
                        "10",
                        "11",
                        "12",
                        "13",
                        "14",
                        "15",
                        "16",
                        "17",
                        "18",
                        "19",
                        "20"
                    );
                    $m[$i] = randm($m, $arr, $config['mnum']);
                } else if ($config['mnum'] == 10) {
                    $arr   = array(
                        "01",
                        "02",
                        "03",
                        "04",
                        "05",
                        "06",
                        "07",
                        "08",
                        "09",
                        "10"
                    );
                    $m[$i] = randm($m, $arr, $config['mnum']);
                }
            }
            
            $mstr .= ",m" . ($i + 1) . "='" . $m[$i] . "'";
        }
        $year = date("Y");
        if (strtotime($kjtime) < time()) {
            $sql = "update `$tb_kj` set kjtime='$kjtime',closetime='$closetime',opentime='$opentime'" . $mstr;
        } else {
            $sql = "update `$tb_kj` set kjtime='$kjtime',closetime='$closetime',opentime='$opentime'";
        }
        $sql .= " where qishu='$qishu' and gid='$gid' ";
        if ($msql->query($sql)) {
            include("../func/search.php");
            searchqishu($gid, 60, 1);
            echo 1;
        }
        break;
    case "kjjs":
        set_time_limit(0);
        include("../func/js.php");
        $gid   = $_REQUEST['gid'];
        $qishu = $_REQUEST['qishu'];
        $val   = kjjs($qishu, $gid, transgame($gid, 'fenlei'));
        echo $val;
        break;
    case "searchqishu":
        
        include("../func/search.php");
        
        $gid = $_REQUEST['gid'];
        
        searchqishu($gid, 60, 1);
        break;
    case "exkj":
        $gid  = $_REQUEST['gid'];
        $data = $_REQUEST['data'];
        $data = explode("\n", $data);
        $mnum = transgame($gid, 'mnum');
        foreach ($data as $val) {
            $v     = explode(',', $val);
            $qishu = $v[0];
            if (!is_numeric($qishu))
                break;
            $sql = '';
            for ($i = 1; $i <= $mnum; $i++) {
                if ($i > 1)
                    $sql .= ",";
                $sql .= "m" . $i . "='" . $v[$i] . "'";
            }
            $msql->query("update `$tb_kj` set $sql where qishu='$qishu' and gid='$gid'");
            //echo "update `$tb_kj` set $sql where qishu='$qishu' and gid='$gid'","<BR />";
        }
        echo 1;
        break;
    case "attpeilvs":
        attpeilvs(100);
        break;
    case "attpeilv":
        attpeilv($_REQUEST['gid']);
        break;
    case "kjxx":
        include('../global/page.class.php');
        $tztype   = $_POST['tztype'];
        $qishu    = $_POST['qishu'];
        $orderby  = $_POST['orderby'];
        $sorttype = $_POST['sorttype'];
        $wh       = " gid='$gid'  and qishu='$qishu' ";
        
        if ($tztype != 2) {
            $wh .= " and xtype='$tztype' ";
        }
        
        
        $zcstr = "zc0";
        $sql   = " select count(id) from `$tb_lib` where  $wh ";
        $msql->query($sql);
        $msql->next_record();
        $rcount   = pr0($msql->f(0));
        $psize    = $config['psize1'];
        $thispage = r1($_REQUEST['PB_page']);
        $page     = new page(array(
            'total' => $rcount,
            'perpage' => $psize,
            'nowindex' => $thispage
        ));
        
        $pstr = $page->show(6);
        
        $sql = " select * from `$tb_lib` where $wh ";
        if ($orderby == 'time') {
            $sql .= " order by time $sorttype,id $sorttype";
        } else {
            $sql .= " order by $zcstr*je $sorttype,id $sorttype";
        }
        $sql .= " limit " . ($thispage - 1) * $psize . "," . $psize;
        $msql->query($sql);
        $tz  = array();
        $i   = 0;
        $tmp = array();
        while ($msql->next_record()) {

            $tz[$i]['qishu']  = $msql->f('qishu');
            $tz[$i]['je']     = (float) $msql->f('je');
            $tz[$i]['zcje']   = (float) pr2($msql->f('je') * $msql->f($zcstr) / 100);
            $tz[$i]['peilv1'] = (float) $msql->f('peilv1');
            
            $tz[$i]['points'] = (float) $msql->f('points');
            

            
            if ($tmp['g' . $msql->f('gid')] == '') {
                $tmp['g' . $msql->f('gid')] = transgame($msql->f('gid'), 'gname');
            }
            if ($tmp['b' . $msql->f('gid') . $msql->f('bid')] == '') {
                $tmp['b' . $msql->f('gid') . $msql->f('bid')] = transb8('name', $msql->f('bid'), $msql->f('gid'));
            }
            if ($tmp['s' . $msql->f('gid') . $msql->f('sid')] == '') {
                $tmp['s' . $msql->f('gid') . $msql->f('sid')] = transs8('name', $msql->f('sid'), $msql->f('gid'));
            }
            if ($tmp['c' . $msql->f('gid') . $msql->f('cid')] == '') {
                $tmp['c' . $msql->f('gid') . $msql->f('cid')] = transc8('name', $msql->f('cid'), $msql->f('gid'));
            }
            if ($tmp['p' . $msql->f('gid') . $msql->f('pid')] == '') {
                $tmp['p' . $msql->f('gid') . $msql->f('pid')] = transp8('name', $msql->f('pid'), $msql->f('gid'));
            }
            $tz[$i]['con']   = $msql->f('content');
            $tz[$i]['wf']    = wf($msql->f('gid'), $tmp['b' . $msql->f('gid') . $msql->f('bid')], $tmp['s' . $msql->f('gid') . $msql->f('sid')], $tmp['c' . $msql->f('gid') . $msql->f('cid')], $tmp['p' . $msql->f('gid') . $msql->f('pid')]);
            $tz[$i]['time']  = $msql->f('time');
            $tz[$i]['z']     = $msql->f('z');
            $tz[$i]['gname'] = $tmp['g' . $msql->f('gid')];
            $tz[$i]['xtime'] = substr($msql->f('time'), -8);
            $tz[$i]['user']  = transu($msql->f('userid'));
            
            $i++;
        }
        $e = array(
            "tz" => $tz,
            "page" => $pstr,
            "tztype" => $tztype,
            "sql" => $sql
        );
        echo json_encode($e);
        unset($e);
        
        unset($tmp);
        break;
}
function rm($v)
{
    if (!is_numeric($v))
        return 49;
    if ($v < 10 & strlen($v) == 1)
        $v = '0' . $v;
    return $v;
}
function closepan()
{
    global $tsql, $tb_config;
    $tsql->query("update `$tb_config` set tepan=0,otherpan=0,autoopenpan=0");
    echo 1;
}
?>